package de.melistik.smugmugsync;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JToolBar;

import net.miginfocom.swing.MigLayout;
import de.melistik.IconHandler;
import de.melistik.smugmugsync.connect.SmugMugBaseRW;
import de.melistik.smugmugsync.connect.SmugMugConnection;
import de.melistik.smugmugsync.connect.SmugMugUpload;
import de.melistik.smugmugsync.model.SyncFolder;

@SuppressWarnings("serial")
public class UploadDialog extends JFrame {

	JLabel folderSyncLabel = new JLabel();
	JLabel currentUploadFolderLabel = new JLabel();
	JLabel currentFilerLabel = new JLabel();
	JProgressBar progessBar = new JProgressBar(0, 100);

	SmugMugBaseRW rwSmugBase;
	SmugMugConnection connection;
	List<SyncFolder> folders2Sync;
	SmugMugUpload uploader = null;

	JButton pauseStartButton;
	boolean holdUpload = true;

	public UploadDialog(SmugMugConnection connet, List<SyncFolder> folders2Sync) {
		this.connection = connet;
		this.folders2Sync = folders2Sync;

		this.setTitle("jSmugSync Upload");
		this.setIconImage(IconHandler.update.getImage());
		this.setName("jSmugSyncUpload");
		this.setLayout(new BorderLayout());
		this.setLocationRelativeTo(null);
		this.setSize(600, 300);
		this.setResizable(false);
		this.setLocation(
				(Toolkit.getDefaultToolkit().getScreenSize().width / 2)
						- (this.getWidth() / 2), (Toolkit.getDefaultToolkit()
						.getScreenSize().height / 2) - (this.getHeight() / 2));

		JPanel formPanel = new JPanel(new MigLayout("", "[pref!][grow,fill]",
				"[]15[]"));

		formPanel.add(new JLabel("Folders to Sync:"));
		formPanel.add(folderSyncLabel, "wrap");

		formPanel.add(new JLabel("Current upload folder:"));
		formPanel.add(currentUploadFolderLabel, "wrap");
		formPanel.add(new JLabel("Current processing File:"));
		formPanel.add(currentFilerLabel, "wrap");

		formPanel.add(new JLabel("Processbar folder:"));
		formPanel.add(progessBar, "wrap");

		add(formPanel, BorderLayout.CENTER);

		JToolBar bottomBar = new JToolBar();
		bottomBar.setFloatable(false);

		pauseStartButton = new JButton("Start", IconHandler.progressbar);
		pauseStartButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!holdUpload) {
					holdUpload = true;
					pauseStartButton.setText("Start");
				} else {
					holdUpload = false;
					pauseStartButton.setText("Pause");
					startProcess();
				}
			}
		});
		bottomBar.add(pauseStartButton);
		JButton cancleButton = new JButton("Cancle", IconHandler.exclamation);
		cancleButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				UploadDialog.this.setVisible(false);
			}
		});
		bottomBar.add(cancleButton);
		this.add(bottomBar, BorderLayout.SOUTH);
	}

	private void startProcess() {
		if (rwSmugBase == null)
			rwSmugBase = new SmugMugBaseRW(connection);
		if (uploader == null)
			uploader = new SmugMugUpload(connection);
		int totalFolderCount = folders2Sync.size();

		for (int folderIdx = 0; folderIdx < totalFolderCount; folderIdx++) {
			if (holdUpload)
				return;
			SyncFolder folder = folders2Sync.get(folderIdx);
			folderSyncLabel.setText(String.format("%s / %s", folderIdx + 1,
					totalFolderCount));

			currentUploadFolderLabel.setText(folder.getFolder()
					.getAbsolutePath());
			int totalPicCount = folder.getImagesInFolder().size();
			int categoryID = rwSmugBase.findOrCreateCategory(folder
					.getCategoryName());
			folder.setAlbumId(rwSmugBase.findOrCreateAlbum(
					folder.getAlbumName(), categoryID));
			progessBar.setValue(0);
			for (int picIdx = 0; picIdx < folder.getImagesInFolder().size(); picIdx++) {
				File currentPic = folder.getImagesInFolder().get(picIdx);
				currentFilerLabel.setText(currentPic.getName());
				if (uploader.uploadFile(currentPic, folder.getAlbumId()) != null) {
					progessBar.setValue(picIdx / totalPicCount);
				} else {
					System.out.println("we had a error with pic: "
							+ currentPic.getAbsolutePath());
				}
				if (holdUpload)
					return;
			}
		}
	}
}
